const { UUIDV4, UUID, STRING, INTEGER, TINYINT, DOUBLE, JSON, Model } = require('sequelize');
const db = require('../../helpers/db');
const Cart = require('../order/cart');

class GoodsSKU extends Model {}

GoodsSKU.init(
    {
        id: {
            type: UUID,
            defaultValue: UUIDV4,
            primaryKey: true,
            comment: '商品SKU ID'
        },
        goods: {
            type: UUID,
            unique: true,
            comment: '外键，商品ID'
        },
        sku: {
            type: JSON,
            allowNull: false,
            comment: '商品SKU集合--展示用'
        },
        cover: {
            type: STRING(128),
            allowNull: false,
            comment: '默认封面图'
        },
        price: {
            type: DOUBLE,
            allowNull: false,
            comment: '价格'
        },
        marketPrice: {
            type: DOUBLE,
            comment: '市场价',
            allowNull: false,
            field: 'market_price'
        },
        name: {
            type: STRING(128),
            allowNull: false,
            comment: '商品名'
        },
        description: {
            type: STRING(128),
            allowNull: false,
            comment: '描述'
        },
        inventory: {
            type: INTEGER,
            comment: '商品总库存',
            defaultValue: 0
        },
        sales: {
            type: INTEGER,
            comment: '商品总销量',
            defaultValue: 0
        }
    },
    {
        tableName: 'goods_sku',
        underscored: true,
        sequelize: db
    }
);

GoodsSKU.hasMany(Cart, {
    foreignKey: 'goodsSKU'
});

module.exports = GoodsSKU;
